<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100px" align="center"></el-table-column>
      <el-table-column label="LOGO" width="100px" align="center">
        <template>
          <div class="demo-type">
            <el-avatar :size="60" :src="tableData.logo" @error="errorHandler">
              <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
            </el-avatar>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" width="300" align="center" header-align="center"></el-table-column>
      <el-table-column prop="pinyin" label="拼音" align="center" header-align="center"></el-table-column>
      <el-table-column prop="description" label="简介" align="center" header-align="center"></el-table-column>
      <el-table-column prop="keywords" label="关键词列表" align="center" header-align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100px" align="center" header-align="center"></el-table-column>
      <el-table-column label="是否启用" width="100px" align="center" header-align="center">
        <template slot-scope="scope">
          <el-switch
              v-model="scope.row.enable"
              @change="toggleEnable(scope.row)"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"
                     @click="openEditDialog(scope.row)" circle>编辑
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"
                     @click="confirmDelete(scope.row)" circle>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  弹出编辑品牌的弹框  -->
    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="名称" label-width="120px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="拼音" label-width="120px">
          <el-input v-model="editForm.pinyin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="LOGO" label-width="120px">
          <el-input v-model="editForm.logo" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="简介" label-width="120px">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="关键词" label-width="120px">
          <el-input v-model="editForm.keywords" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序序号" label-width="120px">
          <el-input v-model="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="是否启用">
          <el-switch
              v-model="editForm.enable"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button @click="submitEditForm()" type="primary">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id:'',
        name: '',
        pinyin:'',
        logo:'',
        description:'',
        sort:'',
        keywords:'',
        enable: ''
      },
    }
  },
  methods:{
    //头像
    errorHandler() {
      return true
    },
    //列表展示
    BrandList(){
      let url = 'http://localhost:8080/brand/list';
      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.tableData = responseBody.data;
        }
      })
    },

    //弹出删除确认框
    confirmDelete(brand) {
      console.log('尝试打开删除[id='+brand.id+']数据的对话框');
      let message = '此操作将永久删除'+brand.name+'品牌，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand);
      }).catch(() => {
      });
    },

    //执行删除
    handleDelete(brand){
      let url = 'http://localhost:8080/brand/'+brand.id+'/delete';
      console.log(url);
      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.$message.success('删除成功！');
          this.BrandList();
        }else if (responseBody.state==40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.BrandList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },

    //弹出编辑框
    openEditDialog(brand){
      console.log('尝试打开编辑【id='+brand.id+'】数据的对话框');
      let url = 'http://localhost:8080/brand/'+brand.id+'/select';
      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state==20000){
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        }else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.BrandList();
            }
          });
        }
      })
    },

    //提交修改
    submitEditForm(){
      let formData = this.qs.stringify(this.editForm);
      let url = 'http://localhost:8080/brand/'+this.editForm.id+'/update';
      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state==20000){
          this.$message.success("修改品牌成功！");
          this.dialogFormVisible = false;
          this.BrandList();
        }else if (responseBody.state==40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.BrandList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },
    //执行启用或禁用
    toggleEnable(brand){
      let enableText = ['禁用','启用'];
      let url = 'http://localhost:8080/brand/'+brand.id+'/';
      if (brand.enable == 1){
        url += 'enable';
      }else {
        url += 'disable';
      }
      console.log('url=' + url);

      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) =>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          let message = '操作成功，已经将【' + brand.name
              + '】的状态改为【' + enableText[brand.enable] + '】！';
          this.$message.success(message);
        }else {
          this.$message.error(responseBody.message);
          this.BrandList();
        }
      })
    }
  },
  mounted() {
    this.BrandList();
  }
}
</script>

<style>

</style>